package controller

import (
	"github.com/gin-gonic/gin"
	"log"
	"websocket/commom"
	"websocket/dto"
	"websocket/model"
	"websocket/response"
)

//搜索好友
func ScreenFriendList(c *gin.Context) {

	db := commom.GetDB()
	tx := db.Begin()
	screen := c.Query("screen")
	condition := "CONCAT(IFNULL(`id`,''),IFNULL(`username`,'')) like '%" + screen + "%'"
	friendList, err := model.GetUserList1(tx, condition)
	if err != nil {
		response.FailWithMessage("查询用户信息失败", c)
		log.Printf("friend generate error : %v", err)
		return
	}
	var userList []dto.SelectFriendList
	for _, value := range friendList {
		var friend dto.SelectFriendList
		friend.UserName = value.UserName
		friend.IsOnline = value.IsOnline
		friend.Image = value.Image
		friend.UserId = value.Id
		userList = append(userList, friend)
	}
	// 遇到错误时回滚事务
	tx.Rollback()

	// 否则，提交事务
	tx.Commit()
	//返回结果
	response.OkWithData(gin.H{"userList": userList}, c)
}

//新增好友分组
func AddFriendList(c *gin.Context) {
	friendsGroup := model.FriendsGroup1{}
	c.BindJSON(&friendsGroup)
	user, _ := c.Get("user")
	user1 := user.(model.User)
	friends := commom.GetMongoDB("friends_group")
	friendsGroup.UserId = user1.Id
	friendsGroup.IsDelete = 0
	err := friendsGroup.FriendsGroupInsert(friends)
	if err != nil {
		response.FailWithMessage("新增好友失败", c)
		log.Printf("addfriend generate error : %v", err)
		return
	}
	response.OkWithData(gin.H{
		"message": "新增成功",
	}, c)
}
func GetFriendList(c *gin.Context) {

}
